<template>
  <q-page class="cc-admin">

    <div class="col bg-white shadow-2 q-pa-md q-ma-sm">
      <q-table flat color="primary" class="cross_table" separator="cell" :columns="columns" :data="list" row-key="id" :pagination.sync="pagination" :visible-columns="group" @request="query" :rows-per-page-options="[10, 20, 50, 100]" selection="multiple" :selected.sync="selected">

        <template v-slot:top="">
          <div class="row no-wrap full-width">
            <q-input clearable outlined dense placeholder="请输入关键字搜索" class="on-left" @input="query" debounce="500" v-model="key">
              <template #append>
                <q-btn flat round icon="search" color="primary" @click="query" :loading="loading">
                  <q-tooltip>搜索</q-tooltip>
                </q-btn>
              </template>
            </q-input>
            <q-space />
            <div class="q-gutter-xs">
              <q-btn icon="add" no-wrap color="primary" label="新建" @click="add" />

            </div>
          </div>
        </template>
        <template #body-cell-opt="props">
          <q-td :props="props" :auto-width="true">
            <q-btn flat round dense color="primary" icon="edit" @click="edit(props.row)"> <q-tooltip>编辑</q-tooltip></q-btn>
            <q-btn flat round dense color="primary" icon="mdi-content-copy" @click="copy(props.row)">
              <q-tooltip>复制</q-tooltip>
            </q-btn>
            <q-btn flat round dense color="primary" icon="mdi-alpha-t-box" @click="test(props.row)"> <q-tooltip>测试</q-tooltip></q-btn>
            <btn-del label="海洋天气api" @confirm="del(props.row)" />
          </q-td>
        </template>
      </q-table>
    </div>
    <q-dialog maximized flat persistent ref="dialog">
      <q-form @submit="submit" class="dialog_card column">
        <h5 class="view_title justify-between q-px-md">
          {{ editType }}海洋天气api
          <q-btn dense outline round icon="clear" size="sm" v-close-popup />
        </h5>
        <q-scroll-area class="col">
          <div class="row q-col-gutter-x-md dialog_form q-pa-md">
            <div class="col-12">
              <h5> 系统平台名称</h5>
              <q-input outlined dense v-model="form.name" type="text" />
            </div>
          </div>
        </q-scroll-area>
        <div class="row justify-center q-pa-md">
          <q-btn outline color="primary" icon="mdi-close-thick" label="关闭" v-close-popup />
          <q-btn class="q-mx-md" color="primary" icon="mdi-check-bold" label="提交" :loading="loading" type="submit" />
        </div>
      </q-form>
    </q-dialog>
    <q-dialog maximized flat persistent ref="test">
      <q-form @submit="submitWea" class="dialog_card column">
        <h5 class="view_title justify-between q-px-md">
          {{ editType }}海洋天气api
          <q-btn dense outline round icon="clear" size="sm" v-close-popup />
        </h5>
        <q-scroll-area class="col">
          <div class="row q-col-gutter-x-md dialog_form q-pa-md">
            <div class="col-3">
              <h5> URL路径</h5>
              <q-input outlined dense v-model="form.url" type="text" />
            </div>
            <div class="col-3">
              <h5> Token参数</h5>
              <q-input outlined dense v-model="form.token" type="text" />
            </div>
            <div class=" col-3">
              <div class="dynamic-form-item ">
                <div v-show="true" class="" style="margin-top: 10px;">
                  <q-icon name="star" color="red" v-show="true" />
                  天气类型
                </div>
                <select-search :formData="form" formKey="type" dense debounce="500" hide-bottom-space outlined v-model="form.type" :options="typeOptions" type="select" lazy-rules :rules="[requireTest]" clearable emit-value map-options @input="requestUrls(form.type)" />
              </div>
            </div>
            <div class="col-3">
              <h5> 坐标</h5>
              <q-input outlined dense v-model="form.location" type="text" />
            </div>
            <div class="col-12">
              <h5> 请求访问地址</h5>
              <q-input outlined dense v-model="form.requestUrl" type="text" readonly />
            </div>
            <div class="col-12">
              <h5> 返回数据</h5>
              <q-input outlined dense v-model="returnData" type="textarea" readonly rows="10" />
            </div>
          </div>
        </q-scroll-area>
        <div class="row justify-center q-pa-md">
          <q-btn outline color="primary" icon="mdi-close-thick" label="关闭" v-close-popup />
          <q-btn class="q-mx-md" color="primary" icon="mdi-check-bold" label="提交" :loading="loading" type="submit" />
        </div>
      </q-form>
    </q-dialog>
    <confirm ref="confirmDialog" :msg="confirmMsg" @confirm="deleteBatch()" />
    <q-inner-loading :showing="loading">
      <q-spinner-gears size="50px" color="primary" />
    </q-inner-loading>
  </q-page>
</template>

<script>
import { IndexMixin } from 'boot/mixins';
import { getDictLabel } from 'boot/dictionary';
import confirm from 'components/confirm';
import { result } from 'lodash';

export default {
  name: 'WeaApi',
  mixins: [IndexMixin],
  components: {
    confirm,
  },
  data() {
    return {
      typeOptions: [
        {
          label: '海洋天气实况',
          value: 'SEA_CONDITIONS'
        }, {
          label: '海啸预警',
          value: 'TSUNAMI_WARNING_LIST'
        }, {
          label: '海冰预警',
          value: 'SEA_ICE_WARNING'
        }, {
          label: '风暴潮预警',
          value: 'STORM_WARNING',
        }, {
          label: '海浪预警',
          value: 'WAVE_WARNING',
        }, {
          label: '台风实况轨迹',
          value: 'TROPICAL_CYCLONE_TRACK',
        }, {
          label: '台风预测',
          value: 'TROPICAL_CYCLONE_FORECAST',
        }, {
          label: '海雾预警',
          value: 'SEA_FOG',
        }, {
          label: '7天预报',
          value: 'SEVEN_DAY_FORECAST',
        }, {
          label: '台风',
          value: 'TROPICAL_CYCLONE',
        }, {
          label: '台风分类',
          value: 'TROPICAL_CYCLONE_YEAR',
        }],
      columns: [
        {
          name: 'index',
          align: 'center',
          label: '序号',
          field: 'index',
        },
        {
          name: 'name', align: 'left', label: '系统名称', field: 'name',
        },
        {
          name: 'token', align: 'left', label: 'Token参数', field: 'token',
        },
        {
          name: 'status', align: 'left', label: '状态', field: 'status',
        },
        {
          name: 'visitCount', align: 'left', label: '访问次数', field: 'visitCount',
        },
        {
          name: 'opt', align: 'center', label: '操作', field: 'id',
        },
      ],
      returnData: [],
      form: {
        url: '',
        token: '',
        type: '',
        location: '',
        requestUrl: '',

      },
      emptyForm: {
        url: process.env.SERVER_URL,
        token: '',
        type: '',
        location: '',

      },
      showQuery: true,
      headers: [{ name: 'Authorization', value: localStorage.Authorization }],
      uploadUrl: `${process.env.SERVER_URL}${process.env.BASE_URL}/sys/common/upload`,
      imgUrl: `${process.env.SERVER_URL}${process.env.BASE_URL}/sys/common/static`,
      url: {
        list: '/wea/api/list',
        add: '/wea/api/add',
        edit: '/wea/api/edit',
        copy:
          '/wea/api/copy',
        delete: '/wea/api/delete',
        deleteBatch: '/wea/api/deleteBatch',
        exportXlsUrl: '/wea/api/exportXls',
        importExcelUrl: '/wea/api/importExcel',

      },
    };
  },
  methods: {
    requireTest(val) {
      return val === 0 || (val && ((`${val}`).length > 0)) || '这是必填字段';
    },
    getDictLabel,
    initDict() {
    },
    test(row) {
      this.$axios.get('/wea/api/queryById', {
        params: {
          id: row.id,
        }
      }).then(({ result }) => {
        this.form = {
          ...result,
        };
        this.form.url = process.env.SERVER_URL;
        this.form.location = '119.316895,39.597702';
        const url = this.form.url || '';
        const token = this.form.token || '';
        const type = this.form.type || '';
        const location = this.form.location || '';
        this.form.requestUrl = `${url}/wea/api/getWeather?token=${token}&type=${type}`;
        this.$refs.test.show();
      });
    },
    submitWea() {
      console.log(this.form);
      this.$axios.get('/wea/api/getWeather', {
        params: {
          token: this.form.token,
          type: this.form.type,
          location: this.form.location
        }
      }).then((result) => {
        console.log(result);
        this.returnData = result.message;
      });
    },
    requestUrls(types) {
      console.log(types);
      const url = this.form.url || '';
      const token = this.form.token || '';
      const type = types || '';
      const location = this.form.location || '';
      if (type === 'SEA_FOG' || type === 'SEA_CONDITIONS' || type === 'SEVEN_DAY_FORECAST') {
        this.form.requestUrl = `${url}/wea/api/getWeather?token=${token}&type=${type}&location=${location}`;
      } else {
        this.form.requestUrl = `${url}/wea/api/getWeather?token=${token}&type=${type}`;
      }
    }
  },
  mounted() {
    this.initDict();
  },
  computed: {
    /* requestUrl() {
      const url = this.form.url || '';
      const token = this.form.token || '';
      const type = this.form.type || '';
      const location = this.form.location || '';
      return `${url}?token=${token}&type=${type}&location=${location}`;
    } */
  }
};

</script>

<style lang="stylus">

</style>
